package com.mytool.domain;

import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Data
public class ColumnDomain {

    private String columnName;
    private String columnType;
    private String columnComment;

    private Class<?> javaType;

    private String javaTypeStr;

    public Class<?> getJavaType () {
        return TYPE_MAPPING.get(columnType);
    }

    public String getJavaTypeStr() {
        if(null == columnType)
            return null;
        return TYPE_STR_MAPPING.get(columnType.toUpperCase());
    }

    public static final Map<String, Class<?>> TYPE_MAPPING = new HashMap<>();

    static {
        // 基础类型映射
        TYPE_MAPPING.put("INT", Integer.class);
        TYPE_MAPPING.put("TINYINT", Integer.class);
        TYPE_MAPPING.put("SMALLINT", Integer.class);
        TYPE_MAPPING.put("MEDIUMINT", Integer.class);
        TYPE_MAPPING.put("INTEGER", Integer.class);
        TYPE_MAPPING.put("BIGINT", Long.class);

        //浮点型
        TYPE_MAPPING.put("FLOAT", Float.class);
        TYPE_MAPPING.put("DOUBLE", Double.class);
        TYPE_MAPPING.put("DECIMAL", BigDecimal.class);
        TYPE_MAPPING.put("NUMERIC", BigDecimal.class);

        //字符串类型
        TYPE_MAPPING.put("CHAR", String.class);
        TYPE_MAPPING.put("VARCHAR", String.class);
        TYPE_MAPPING.put("TINYTEXT", String.class);
        TYPE_MAPPING.put("TEXT", String.class);
        TYPE_MAPPING.put("MEDIUMTEXT", String.class);
        TYPE_MAPPING.put("LONGTEXT", String.class);

        //日期型
        TYPE_MAPPING.put("DATE", LocalDate.class);
        TYPE_MAPPING.put("TIME", LocalTime.class);
        TYPE_MAPPING.put("DATETIME", LocalDateTime.class);
        TYPE_MAPPING.put("TIMESTAMP", LocalDateTime.class);
        TYPE_MAPPING.put("BOOLEAN", Boolean.class);

        //其它类型
        TYPE_MAPPING.put("TINYINT(1)", Boolean.class);
        TYPE_MAPPING.put("BLOB", byte[].class);
        TYPE_MAPPING.put("ENUM", String.class);
    }

    public static final Map<String, String> TYPE_STR_MAPPING = new HashMap<>();

    static {
        TYPE_STR_MAPPING.put("INT", "Integer");
        TYPE_STR_MAPPING.put("TINYINT", "Integer");
        TYPE_STR_MAPPING.put("SMALLINT", "Integer");
        TYPE_STR_MAPPING.put("MEDIUMINT", "Integer");
        TYPE_STR_MAPPING.put("INTEGER", "Integer");
        TYPE_STR_MAPPING.put("BIGINT", "Long");

        //浮点型
        TYPE_STR_MAPPING.put("FLOAT", "Float");
        TYPE_STR_MAPPING.put("DOUBLE", "Double");
        TYPE_STR_MAPPING.put("DECIMAL", "BigDecimal");
        TYPE_STR_MAPPING.put("NUMERIC", "BigDecimal");

        //字符串类型
        TYPE_STR_MAPPING.put("CHAR", "String");
        TYPE_STR_MAPPING.put("VARCHAR", "String");
        TYPE_STR_MAPPING.put("TINYTEXT", "String");
        TYPE_STR_MAPPING.put("TEXT", "String");
        TYPE_STR_MAPPING.put("MEDIUMTEXT", "String");
        TYPE_STR_MAPPING.put("LONGTEXT", "String");

        //日期型
        TYPE_STR_MAPPING.put("DATE", "LocalDate");
        TYPE_STR_MAPPING.put("TIME", "LocalTime");
        TYPE_STR_MAPPING.put("DATETIME", "LocalDateTime");
        TYPE_STR_MAPPING.put("TIMESTAMP", "LocalDateTime");
        TYPE_STR_MAPPING.put("BOOLEAN", "Boolean");

        //其它类型
        TYPE_STR_MAPPING.put("TINYINT(1)", "Boolean");
        TYPE_STR_MAPPING.put("BLOB", "byte[]");
        TYPE_STR_MAPPING.put("ENUM", "String");
    }

}
